<template>
	<view class="content">
		<view class="add-box">
			<u-form :model="form" ref="uForm">
				<u-form-item label-width="160" label="车牌号码" prop="carNumber">
					<u-input v-model="form.carNumber" placeholder="请输入车牌" />
				</u-form-item>
				<u-form-item label-width="160" label="司机姓名" prop="driverName">
					<u-input v-model="form.driverName" placeholder="请输入司机姓名" />
				</u-form-item>
				<u-form-item label-width="160" label="联系电话" prop="driverPhone">
					<u-input v-model="form.driverPhone" placeholder="请输入司机联系电话" />
				</u-form-item>
<!-- 				<u-form-item label-width="160" label="作业点" >
					<u-input v-model="siteId" @click="selectSiteShow = true" placeholder="请选择作业点" type="select" />
				</u-form-item> -->
				<u-form-item label-width="160" label="一号摄像头" prop="frontCameraAddress">
					<u-input v-model="form.frontCameraAddress" placeholder="请输入摄像头地址" :clearable="false" />
					<u-icon name="camera-fill" color="#00aaff" size="40rpx" @tap="photo(1)"></u-icon>
				</u-form-item>
				<u-form-item label-width="160" label="二号摄像头" prop="backCameraAddress">
					<u-input v-model="form.backCameraAddress" placeholder="请输入摄像头地址" :clearable="false" />
					<u-icon name="camera-fill" color="#00aaff" size="40rpx" @tap="photo(2)"></u-icon>
				</u-form-item>
				<u-form-item label-width="160" label="图片上传" >
					<u-upload width="160" height="160" :form-data="queryOss" :action="action" :auto-upload="false" ref="uUpload"
					 max-count="1" :show-progress="false" :multiple="false" @on-choose-complete="changeImg()"></u-upload>
				</u-form-item>
			</u-form>
		</view>
		<view class="button-box">
			<u-button type="primary" @tap="addCar" :disabled="isclick">添加</u-button>
		</view>
		<view class="nav" v-show="isclick">
			<navigator url="/pages/project/pointMangement/addSite/index" hover-class="navigator-hover">
				<view>没有作业点？前往添加 &nbsp; <u-icon name="arrow-right"></u-icon></view>
			</navigator>
		</view>
		<u-select mode="single-column" :list="selectSwitch" v-model="selectTypeShow" @confirm="selectTypeConfirm"></u-select>
		<!-- <u-select mode="single-column" :list="selectList" v-model="selectSiteShow" @confirm="selectSiteConfirm"></u-select> -->
	</view>
</template>

<script>
	import {
		getSite
	} from "@/api/site.js"
	import {
		createCar
	} from "@/api/car.js"
	import {
		ossimg
	} from '@/api/oss.js'
	export default {
		data() {
			return {
				form: {
					action: 'team',
					carNumber: "",
					driverName: "",
					driverPhone: "",
					type: 2,
					projectId: null,
					// siteId: null,
					frontCameraAddress: null,
					backCameraAddress: null,
					logo: ""
					// iotId: null
				},
				selectShow: false,
				isclick: false,
				siteId: '',
				type: '',
				selectTypeShow: false,
				selectSiteShow: false,
				selectList: [],
				action: '',
				queryOss: {},
				dir:'',
				selectSwitch: [{
						label: "运输车",
						value: 1
					},
					{
						label: "挖掘机",
						value: 2
					}
				],
				rules: {
					carNumber: [{
						required: true,
						message: '请输入车牌号',
						trigger: ['blur'],
					}],
					carType: [{
						required: true,
						message: '请输入车辆类型',
						trigger: ['blur'],
					}],
					frontCameraAddress: [{
						required: true,
						message: '请输入前置视角地址',
						trigger: ['blur'],
					}],
					iotid: [{
						required: true,
						message: '请绑定设备',
						trigger: ['blur'],
					}],
					backCameraAddress: [{
						required: true,
						message: '请输入后置视角地址',
						trigger: ['blur'],
					}],
					driverName: [{
						required: true,
						message: '请输入司机姓名',
						trigger: ['blur'],
					}],
					driverPhone: [{
						required: true,
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// this.$u.test.mobile()就是返回true或者false的
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: 'blur',
					}],
				}
			};
		},
		onShow() {
			var pages = getCurrentPages();
			this.prevPage = pages[pages.length - 1];
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		created() {
			this.getOss()
		},
		methods: {
			// 选择商品类型回调
			selectTypeConfirm(e) {
				e.map((val, index) => {
					this.form.type = val.value
					this.type = val.label
				});
			},
			changeImg(event) {
				let files = this.$refs.uUpload.lists;
				this.queryOss.Filename = files[0].file.name
				const imgFormat = files[0].url.split('/').pop().split('.')[1];
				const imgName = files[0].url.split('/').pop().split('.')[0];
				const imgMd5Name = this.$md5(imgName);
				this.queryOss.key = `${this.dir}${imgMd5Name}.${imgFormat}`
				this.form.logo = this.action + '/' + this.queryOss.key
			},
			getOss(){
				ossimg({
					dir: 'car'
				}).then(res => {
					console.log(res);
					this.queryOss = res.result
					this.queryOss.OSSAccessKeyId = res.result.accessid
					const {
						host,
						OSSAccessKeyId,
						policy,
						signature,
						dir
					} = res.result
					this.queryOss = {
						'OSSAccessKeyId': OSSAccessKeyId,
						'policy': policy,
						'signature': signature,
						'success_action_status': '200',
					}
					this.action = res.result.host
					this.dir = dir
					console.log(this.action);
				})
			},
			photo(bool) {
				// console.log(bool)
				uni.scanCode({
					scanType:['qrCode'],
					success: (res) => {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						if (bool === 1) {
							this.form.frontCameraAddress = res.result
						} else if(bool === 2) {
							this.form.backCameraAddress = res.result
						} else{
							this.form.iotId = res.result
						}
					},
					fail: err => {
						uni.showToast({
							title: "扫码失败",
							icon: "none",
							duration: 2000
						})
					}
				});
			},
			addCar() {
				this.form.projectId = uni.getStorageSync('id')
				if (this.form.type === null || this.form.siteId === null || this.form.frontCameraAddress === null || this.form.backCameraAddress ===
					null) {
					uni.showToast({
						title: "不能为空",
						icon: "none"
					})
					return
				}
				this.$refs.uForm.validate(val => {
					if (val) {
						this.$refs.uUpload.upload();
						createCar(this.form).then(res => {
							uni.showToast({
								title: res.msg,
								icon: "none"
							})
							// console.log(res)
							if (res.code !== 1) {
								return
							}
							setTimeout(() => {
								var item = true; //item为要传的值
								var pages = getCurrentPages();
								var prevPage = pages[pages.length - 2];
								prevPage.brand = item;
								uni.navigateBack()
							}, 500)

						})
					}
				})
			}

		}
	};
</script>

<style lang="scss" scoped>
	.content {
		padding: 40rpx;

		.add-box {
			background-color: #ffffff;
			border-radius: 10rpx;
			padding: 20rpx;
		}

		.button-box {
			margin-top: 80rpx;
		}
	}
	.nav{
		text-align: center;
		margin-top: 100rpx;
		color: blue;
	}
</style>
